home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.04 Apr 91 / Code Optimizing / Sources / Test.f < prev    next >
Encoding:
Text File  |  1990-04-29  |  4.5 KB  |  150 lines  |  [TEXT/MPS ]

  1. !!M Inlines.f
  2. C  The compiler directive above gives us access
  3. C  to the Macintosh Toolbox.
  4. C-------------------------------------------------
  5. C  This program determines the time needed to
  6. C  perform matrix multiplcation, and also finds
  7. C  out how the total time is divided between
  8. C  actual floating-point calculations and index-
  9. C  ing overhead.
  10. C
  11. C  April 1990.
  12. C  Jon Bell, Dept. of Physics & Computer Science
  13. C  Presbyterian College, Clinton SC.
  14. C
  15. C  Written in Language Systems FORTRAN, v2.0.
  16. C-------------------------------------------------
  17.       IMPLICIT NONE
  18.       INTEGER I, J
  19.       EXTENDED TOTAL, OVERHEAD, CALCS
  20.       EXTENDED A(3,5), B(5,3), C(3,3)
  21.       DATA ((A(I,J), J=1,5), I=1,3)
  22.      &     /  -2.0, -7.0, -4.0,  8.0,  1.0,
  23.      &        -3.0,  0.0, -1.0,  0.0, -9.0,
  24.      &         0.0, -1.0, -8.0, -9.0, -3.0  /
  25.       DATA ((B(I,J), J=1,3), I=1,5)
  26.      &     /  -1.0,  3.0, -2.0,
  27.      &         4.0, -1.0,  1.0,
  28.      &         9.0, -4.0, -8.0,
  29.      &        -5.0,  0.0, -3.0,
  30.      &         6.0,  3.0, -6.0  /
  31. C
  32. C  First demonstrate that the matrix 
  33. C  multiplication works properly.
  34. C
  35.       CALL MXMPY (A, B, C, 3, 5, 3)
  36.       TYPE *, 'Matrix A is:'
  37.       TYPE *
  38.       CALL MATPRINT (A, 3, 5)
  39.       TYPE *
  40.       TYPE *, 'Matrix B is:'
  41.       TYPE *
  42.       CALL MATPRINT (B, 5, 3)
  43.       TYPE *
  44.       TYPE *, 'Their product is:'
  45.       TYPE *
  46.       CALL MATPRINT (C, 3, 3)
  47.       TYPE *
  48. C
  49. C  Find the time it takes to multiply matrices
  50. C  of various sizes.
  51. C
  52.       TYPE *
  53.       TYPE *, 'Time to multiply two matrices:'
  54.       TYPE *
  55.       TYPE *, '      Size           Total',
  56.      *        '      Overhead          Calcs'
  57.       TYPE *, '----------     -----------',
  58.      *        '    ----------     ----------'
  59.       CALL TIMER (5, TOTAL, OVERHEAD, CALCS)
  60.       TYPE 100, '5 x 5', TOTAL, OVERHEAD, CALCS
  61.       CALL TIMER (10, TOTAL, OVERHEAD, CALCS)
  62.       TYPE 100, '10 x 10', TOTAL, OVERHEAD, CALCS
  63.       CALL TIMER (20, TOTAL, OVERHEAD, CALCS)
  64.       TYPE 100, '20 x 20', TOTAL, OVERHEAD, CALCS
  65.       CALL TIMER (50, TOTAL, OVERHEAD, CALCS)
  66.       TYPE 100, '50 x 50', TOTAL, OVERHEAD, CALCS
  67. 100   FORMAT (1X, A10, 3(5X, F10.3))
  68.       END
  69.  
  70.       SUBROUTINE MATINIT (A, M, N)
  71. C-------------------------------------------------
  72. C  Initialize the array A as a M x N matrix.
  73. C-------------------------------------------------
  74.       IMPLICIT NONE
  75.       INTEGER M, N, I, J
  76.       EXTENDED A(M,N)
  77.       DO I = 1, M
  78.          DO J = 1, N
  79.             A(I,J) = REAL(I+J)
  80.          END DO
  81.       END DO
  82.       END
  83.  
  84.       SUBROUTINE MATPRINT (A, M, N)
  85. C-------------------------------------------------
  86. C  Prints the contents of the M x N matrix A.
  87. C  Both dimensions must not be greater than 10.
  88. C-------------------------------------------------
  89.       IMPLICIT NONE
  90.       INTEGER M, N, I, J
  91.       EXTENDED A(M,N)
  92.       DO I = 1, M
  93.           TYPE '(10F8.1)', (A(I,J), J=1,N)
  94.       END DO
  95.       END
  96.  
  97.       SUBROUTINE TIMER 
  98.      *   (SIZE, TOTAL, OVERHEAD, CALCS)
  99. C-------------------------------------------------
  100. C  Determine the time required to multiply two
  101. C  SIZE x SIZE matrices (where SIZE <= 50).
  102. C     TOTAL = total time (seconds). 
  103. C     OVERHEAD = indexing overhead (seconds).
  104. C     CALCS = actual time spent in float-
  105. C  ing-point calculations (seconds).
  106. C-------------------------------------------------
  107.       IMPLICIT NONE
  108.       INTEGER SIZE, TICKS, STARTTICKS, 
  109.      *        STOPTICKS, J
  110.       EXTENDED TOTAL, OVERHEAD, CALCS
  111.       EXTENDED SOURCE(50,50), RESULT(50,50)
  112.       CALL MATINIT (SOURCE, SIZE, SIZE)
  113. C
  114. C  Multiply the matrix by itself ten times and 
  115. C  find the average time per multiplication.
  116. C  Notice that we're using only part of the 
  117. C  matrix.  Although it's declared here as 50x50,
  118. C  all the other subroutines will think it's
  119. C  really SIZE x SIZE!
  120. C
  121.       TICKS = 0
  122.       DO J = 1, 10
  123.          STARTTICKS = TICKCOUNT()
  124.          CALL MXMPY (SOURCE, SOURCE, RESULT, SIZE,
  125.      *               SIZE, SIZE)
  126.          STOPTICKS = TICKCOUNT()
  127.          TICKS = TICKS + (STOPTICKS - STARTTICKS)
  128.       END DO
  129.       TOTAL = REAL(TICKS) / 600.0
  130. C
  131. C  Repeat with a “dummy” routine which has all the 
  132. C  indexing overhead of the matrix multiplication
  133. C  but doesn’t actually do any arithmetic.
  134. C
  135.       TICKS = 0
  136.       DO J = 1, 10
  137.          STARTTICKS = TICKCOUNT()
  138.          CALL DUMMY (SOURCE, SOURCE, RESULT,
  139.      *                 SIZE, SIZE, SIZE)
  140.          STOPTICKS = TICKCOUNT()
  141.          TICKS = TICKS + (STOPTICKS - STARTTICKS)
  142.       END DO
  143.       OVERHEAD = REAL(TICKS) / 600.0
  144. C
  145. C  Calculate the average time spent doing actual
  146. C  arithmetic.
  147. C
  148.       CALCS = TOTAL - OVERHEAD
  149.       END
  150.